# -*- coding: utf-8 -*-
import socket

EOL1 = b'\n\n'
EOL2 = b'\n\r\n'
body = '''Hello, world! <h1> from the5fire 《Django 企业开发实战》</h1>'''
response_params = [
	'HTTP/1.0 200 OK',
	'Date: Sun, 27 may 2018 01:01:01 GMT',
	'Content-Type: text/plain; charset = utf-8',
	'Content-Length: {}\r\n'.format(len(body.encode())),
	body,
]
response = '\r\n'.join(response_params)

def handle_connection(conn, addr):
	print("xxx")
	requset = b" "
	while EOL1 not in requset and EOL2 not in requset:
		requset += conn.recv(1024)
	print(requset)
	conn.send(response.encode())
	conn.close()

def main():
	# socket.AF_INET
	# socket.SOCK_STREAM
	seversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	seversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	seversocket.bind(('127.0.0.1',8000))
	seversocket.listen(5)
	print('http://127.0.0.1:8000')

	try:
		while True:
			conn, address = seversocket.accept()
			print("xx1")
			handle_connection(conn, address)
	finally:
		seversocket.close()

if __name__ == '__main__':
	main()